package org.sample.model;

import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

public class Pagination<E> implements Serializable {

    public interface All {
    }

    @JsonView(All.class)
    @ApiModelProperty("当前页码")
    private Integer page;

    @JsonView(All.class)
    @ApiModelProperty("总页数")
    private Integer totalPages;

    @JsonView(All.class)
    @ApiModelProperty("总行数")
    private Integer total;

    @JsonView(All.class)
    @ApiModelProperty("是否有下一页")
    private boolean hasNext;

    @JsonView(All.class)
    @ApiModelProperty("数据集合")
    private List<E> list;

    public Pagination() {
    }

    public Pagination(PageInfo pageInfo, List<E> list) {
        if (pageInfo != null) {
            if (pageInfo.isShowTotalPages()) {
                this.hasNext = pageInfo.hasNext();
            } else {
                //若无需展示总页数，则PageInfo中的hasNext 以大于size一条记录的存在与否来决定
                this.hasNext = list.size() > pageInfo.getSize();
                if (hasNext) {
                    list.remove(list.size() - 1);
                }
            }

            this.page = pageInfo.getPage();
            this.totalPages = pageInfo.getTotalPages();
            this.total = pageInfo.getTotal();
        }
        this.list = list;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public boolean isHasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public List<E> getList() {
        return list;
    }

    public void setList(List<E> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        int size = list == null ? 0 : list.size();
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("page:").append(page);
        sb.append(",totalPages:").append(totalPages);
        sb.append(",total:").append(total);
        sb.append(",hasNext:").append(hasNext);
        sb.append(",listSize:").append(size);
        sb.append("}");
        return sb.toString();
    }
}